<?php
//定义系统删除状态，可以修改成自己喜欢的数字
define('DEL_STATUS', -9);
//下面函数名称要是不喜欢这样命名方式，可以修改成驼峰命名形式，调用方法需要对应替换
//传id值和表名获取单条记录
function get_info($id, $name){
	return db_get_obj($name, array('id' => $id));
}
function db_log_queries() {
	$CI =& get_instance();
	$times = $CI->db->query_times;
	$log_str = '';
	foreach ($CI->db->queries as $k => $v ) {
		$log_str .= $k.' '.number_format($times[$k], 4).' '.str_replace("\n", ' ', $v)."\n";
	}
	$log_str .=	'页面打开共使用 时间'.$CI->benchmark->elapsed_time(
			'total_execution_time_start',
			'total_execution_time_end'
		).'秒 内存'.round(memory_get_usage()/1024/1024, 2).'MB'
	;
	log_my('', $log_str, 'sql');
}
function db_table_exists( $table = '' ) {
	$ret = false;

	$CI = &get_instance();
	if ($CI->db->table_exists( $table )) {
		$ret = true;
	}

	return $ret;
}
//根据条件获取表中单条记录
function db_get_obj($table = '', $where = '', $order = '', $offset = 0, $select = '') {
	$CI = &get_instance();
	if( $where != '' ) {
		$CI->db->where($where);
	}
	if( $order != '' ) {
		$CI->db->order_by($order);
	}
	if( $select != '' ) {
		$CI->db->select($select);
	}
	$query = $CI->db->get($table, 1, $offset);
	if ($query->num_rows() > 0) {
		$obj = $query->row_array();
		return $obj;
	} else {
		return false;
	}
}
function db_get_list($table = '', $where = '', $limit = '', $offset = '', $order = '', $select = '', $like = '', $distinct = '', $group_by = '') {
	$CI = &get_instance();
	if( $order != '' ) {
		$CI->db->order_by($order);
	}
	if ( $where != '' ) {
		$CI->db->where($where);
	}
	if( $select != '' ) {
		$CI->db->select($select);
	}
	if( $like != '' ) {
		$CI->db->like($like);
	}
	if( $distinct != '' ) {
		$CI->db->distinct($distinct);
	}
	if( $group_by != '' ) {
		$CI->db->group_by($group_by);
	}
	$limit = $limit > 0 ? $limit : NULL;
	$offset = $offset > 0 ? $offset : NULL;
	$query = $CI->db->get($table, $limit, $offset);
	$ret = $query->result_array();
	$ret = empty($ret) ? array() : $ret;
	return $ret;
}
function db_get_List_by_sql($sql = '') {
	$CI = &get_instance();
	$ret = $CI->db->query($sql);
	$ret = $ret->result_array();
	return $ret;
}
function db_exe_sql($sql = '') {
	$CI = &get_instance();
	$ret = $CI->db->query($sql);
	return $ret;
}
function db_insert($table = '', $data = '') {
	$CI = &get_instance();
	$CI->db->insert($table, $data);
	$insertid = $CI->db->insert_id();
	return $insertid;
}
function db_insert_batch($table = '', $data = '') {
    $CI = &get_instance();
    $CI->db->insert_batch($table, $data);
    return $CI->db->affected_rows();
}
function db_update_batch($table = '', $data = '', $key='') {
    $CI = &get_instance();
    $ret = $CI->db->update_batch($table, $data, $key);
    return $CI->db->affected_rows();
}
function db_update($table = '', $data = '', $where = '', $order = '', $limit = '') {
	$limit = $limit > 0 ? $limit : NULL;
	$CI = &get_instance();
	$ret = $CI->db->update($table, $data, $where, $order, $limit);
	return $ret;
}

function db_delete($table = '', $where = '') {
	$CI = &get_instance();
	$ret = $where == '' ? false : $CI->db->delete($table, $where);
	return $ret;
}
function db_count_record_num($table = '', $where = '', $like = '', $field = 'id') {
	$CI = &get_instance();
	if ( $where != '' ) {
		$CI->db->where($where);
	}
	if( $like != '' ) {
		$CI->db->like($like);
	}
	$CI->db->select($field);
	$ret = $CI->db->count_all_results($table);
	return $ret;
}
function db_sum_record($table = '', $where = '', $sum_field = '', $sum_name = 'num') {
	$CI = &get_instance();
	if ( $where != '' ) {
		$CI->db->where($where);
	}
	$CI->db->select_sum($sum_field, $sum_name);
	$query = $CI->db->get($table);
	if ($query->num_rows() > 0) {
		$obj = $query->row_array();
		$ret = $obj[$sum_name];
		$ret = $ret ? $ret : 0;
	} else {
		$ret = 0;
	}
	return $ret;
}
function db_last_query(){
	$CI = &get_instance();
	return $CI->db->last_query();
}